package com.kitty.game.party.model;

import com.kitty.common.db.BaseEntityGame;
import com.kitty.common.db.Db4CommonService;
import com.kitty.game.bangpai.PartyDeny;
import com.kitty.game.utils.JsonUtils;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;
import org.nutz.dao.entity.annotation.ColDefine;
import org.nutz.dao.entity.annotation.Column;
import org.nutz.dao.entity.annotation.Comment;
import org.nutz.dao.entity.annotation.Id;
import org.nutz.dao.entity.annotation.Index;
import org.nutz.dao.entity.annotation.Table;
import org.nutz.dao.entity.annotation.TableIndexes;
import org.nutz.lang.util.NutMap;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Table("t_party")
@Setter
@Getter
@TableIndexes({@Index(fields = {"partyName"}, unique = false),@Index(fields = {"id"}, unique = false)})
public class Party extends BaseEntityGame<Long> implements Comparable<Party>{
    @Id(auto = false)
    @Comment("帮派ID")
    private Long id;
    @Column
    @Comment("帮派等级")
    private short level;
    @Column
    @Comment("帮派名字")
    private String partyName;
    @Column
    @Comment("创建人")
    private String creater;
    @Column
    @Comment("创建时间")
    private long createTime;
    @Column
    @ColDefine(width = 2048)
    @Comment("帮派职位")
    private NutMap jobs;
    @Column
    @Comment("研发技能")
    @ColDefine(customType = "longtext")
    private List<NutMap> skills;
    @Column
    @Comment("帮派资金")
    private int capital;
    @Column
    @Comment("帮派俸禄")
    private int fenglu;
    @Column
    @Comment("建设度")
    private int jianshe=2000000000;
    @Column
    @Comment("公告")
    @ColDefine(width = 2048)
    private String notice;
    @Column
    @Comment("图标")
    private String icon;
    @Column
    @Comment("帮主继承人")
    private String heir;
    @Column
    @Comment("帮主继承人继承时间")
    private long heirEndTime;

    @Column
    @Comment("自动接受最小等级")
    private short autoAgreeMinLevel;
    @Column
    @Comment("自动接受最大等级")
    private short autoAgreeMaxLevel;
    @Column
    @Comment("自动接受最少道行")
    private int autoAgreeMinTao;
    @Column
    @Comment("自动接受开关")
    private boolean autoAgreeWork;
    @Column
    @ColDefine(customType = "longtext")
    private String membersJson;
    /**帮派成员列表*/
    private Map<Long, PartyMemberObject> members = new ConcurrentHashMap<>();
    /**申请列表*/
    private NutMap reqJoin = new NutMap();

    @Column
    @ColDefine(customType = "longtext")
    private String prevMembersJson;
    /**成员离开后的保存的一些信息，在其下次加入帮派时用到*/
    private Map<Long, PrevPartyMemberObject> prevMembers = new HashMap<>();


    @Column
    @ColDefine(customType = "longtext")
    private String denyJson;
    /**禁言玩家列表*/
    private Map<Long, PartyDeny> denyMap = new ConcurrentHashMap<>();

    @Column
    @ColDefine(customType = "longtext")
    private String logsJson;
    /**帮派日志*/
    private List<PartyLog> logs = new ArrayList<>();
    @Column
    @ColDefine(customType = "longtext")
    private String redBagJson;
    /**帮派红包*/
    private List<PartyRedBag> redBagList;


    @Override
    public Long getId() {
        return id;
    }

    /**投递进入库队列*/
    public void save(){
        Db4CommonService.getInstance().add2Queue(this);
    }

    @Override
    public int compareTo(Party o) {
        return o.getLevel() - getLevel();
    }

    @Override
    public void doAfterInit() {
        if (StringUtils.isNotEmpty(membersJson)) {
            Map<Long, PartyMemberObject> map = JsonUtils.string2Map(membersJson, Long.class, PartyMemberObject.class);
            Map<Long, PartyMemberObject> members = new ConcurrentHashMap<>();
            members.putAll(map);

            this.members = members;
        }

        if (StringUtils.isNotEmpty(prevMembersJson)) {
            Map<Long, PrevPartyMemberObject> map = JsonUtils.string2Map(prevMembersJson, Long.class, PrevPartyMemberObject.class);
            this.prevMembers = map;
        }

        if (StringUtils.isNotEmpty(denyJson)) {
            Map<Long, PartyDeny> map = JsonUtils.string2Map(denyJson, Long.class, PartyDeny.class);
            Map<Long, PartyDeny> denyMap = new ConcurrentHashMap<>();
            denyMap.putAll(map);

            this.denyMap = denyMap;
        }

        if (StringUtils.isNotEmpty(logsJson)) {
            List<PartyLog> logs = JsonUtils.string2Collection(logsJson, ArrayList.class, PartyLog.class);
            this.logs =  logs;
        }

        if (StringUtils.isNotEmpty(redBagJson)) {
            ArrayList redBagList = JsonUtils.string2Collection(redBagJson, ArrayList.class, PartyRedBag.class);
            this.redBagList = redBagList;
        }
    }

    @Override
    public void doBeforeSave() {
        if (members != null) {
            membersJson = JsonUtils.object2String(members);
        }
        if (prevMembers != null) {
            prevMembersJson = JsonUtils.object2String(prevMembers);
        }
        if (denyMap != null) {
            denyJson = JsonUtils.object2String(denyMap);
        }
        if (logs != null) {
            logsJson = JsonUtils.object2String(logs);
        }
        if (redBagList != null) {
            redBagJson = JsonUtils.object2String(redBagList);
        }
    }
}
